<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <el-option
            v-for="item in emloyees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptSubmit">立即创建</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmloyeeSimple, addDepartments, addDepartDetail, editDepartDetail } from '@/api/departments'
export default {
// 方便进行调试
// 标识组件别名
  name: 'DeptDialog',
  props: {
    // 接受点击的id值
    parentId: {
      type: [Number, String],
      required: true
    },
    // 判断是编辑还是添加
    isEdit: {
      type: Boolean,
      required: true
    },
    // 父亲传递精简过后的数据
    originList: {
      type: [Array],
      required: true
    }
  },
  data() {
    // 验证编码是否存在，编码在整个验证中不允许重复
    const valideCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      // 当编辑时，已存在code值
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      existCodeList.includes(value) ? callback(new Error(`部门编码${value}已经存在`)) : callback()
    }
    // 验证同级部门名字是否相同
    const valideName = (rule, value, callback) => {
      // 查找同级的分类
      let existNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      if (this.isEdit) {
        // 查找同级的分类
        const obj = this.originList.find(item => item.id === this.parentId)
        // 获取到本身以后
        const pid = obj.pid
        // 根据pid找到同级
        existNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }

      // 判断最新输入的部门名称是否存在
      existNameList.includes(value) ? callback(new Error('名字' + value + '已经被占用了')) : callback()
    }
    return {
      // 表单内容
      form: {
        name: '',
        code: '',
        introduce: '',
        manger: ''
      },
      emloyees: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: valideName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: valideCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.getEmloyeeSimple()
    this.addDepartDetail()
  },
  methods: {
    // 点击确定方法
    async deptSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.isEdit ? this.editDepartDetail() : this.addDepartment()
        // 传事件更新
        this.$emit('update-list')
      })
    },
    // 点击取消
    cancel() {
      this.$emit('close')
    },
    // 获取负责人信息
    async  getEmloyeeSimple() {
      const res = await getEmloyeeSimple()
      console.log(res)
      this.emloyees = res.data
    },
    // 查询部门信息
    async addDepartDetail() {
      if (this.isEdit) {
        const res = await addDepartDetail(this.parentId)
        console.log(res)
        this.form = res.data
      }
    },
    // 增加部门信息
    // 调用增加接口
    async addDepartment() {
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      console.log(res)
      // 给用户提示
      this.$message.success(res.message)
    },
    // 修改部门信息
    async editDepartDetail() {
      const res = await editDepartDetail(this.form)
      console.log(res)
      // 给用户提示
      this.$message.success(res.message)
    }
  }
}
</script>

<style></style>
